/**
 * @param {number} n
 * @return {number[]}
 */
var countBits = function (n) {
  function getNums(num) {
    if (num == 1) return 1
    let count = 0
    while (Math.floor(num / 2) >= 1) {
      count += num % 2 == 0 ? 0 : 1
      num = Math.floor(num / 2)
    }
    if (num == 1) count += 1
    return count
  }
  let arr = []
  for (let i = 0; i <= n; i++) {
    arr.push(getNums(i))
  }
  return arr
}
console.log(countBits(5));